
// Create the canvas

var canvas = document.createElement("canvas");
var ctx = canvas.getContext("2d");
canvas.width = 1200;
canvas.height = 600;
document.body.appendChild(canvas);


// Game objects
var player = { speed: 256, // movement in pixels per second
x: 0, y: 0
};
var enemy1 = [];
var enemy2 = [];
var enemy3 = [];
var bullets= [];
var gametime = 0;
var c = 0;
var q = 0;
var score = 0;
var dead = false;
var hscore = 0;
var es1;
var es2;
var es3;
var lvl;
var isnuke = true;
var newgm = true;
var spwn1 = 1;
var spwn2 = 1;
var spwn3 = 1;
var bready = true;
// Handle keyboard controls
var keysDown = {};
addEventListener("keydown", function (e) { keysDown[e.keyCode] = true; }, false);
addEventListener("keyup", function (e) { delete keysDown[e.keyCode]; }, false);

// Reset the game when the player catches a monster
var reset = function () {
player.x = 0; player.y = canvas.height / 2;
if(hscore==0){hscore=score;}
if(hscore<score){hscore=score;}
// Throw the monster somewhere on the screen randomly
enemy1 = [];
enemy2 = [];
enemy3 = [];
bullets = [];
gametime = 0;
c = 0;
q=0;
score = 0;
isnuke = true;
dead=false;
newgm==true;
 spwn1 = 1;
spwn2 = 1;
spwn3 = 1;
bready = true;
};
// Update game objects
var update = function (modifier) {
	if(newgm==true){
		
	if(32 in keysDown)
	{
		if(newgm==true){
			newgm=false;
			
			}
	}
	}
	if(newgm == false){
		if(32 in keysDown&&bready == true&&score>200){score = score-200;bready=false;cbullet(bullets,player.x,player.y+22);}
if (38 in keysDown) { // Playerr.x, holding up
player.y -= player.speed * modifier;
if(player.y<=0)player.y=0; }
if (40 in keysDown) { // Player holding down
player.y += player.speed * modifier; 
if(player.y>=560)player.y=560; }
if (37 in keysDown) { // Player holding left
player.x -= player.speed * modifier; 
if(player.x<=0)player.x=0;}
if (39 in keysDown) { // Player holding right
player.x += player.speed * modifier; 
if(player.x>=1140)player.x=1140;}
gametime++;
score++;
spawn();

if(score < 5000){es1=200;es2=130;es3=80;}
if(score == 5000){gametime=0;es1=270;es2=180;es3=130;}
if(score > 5000 && score < 12000){es1=270;es2=180;es3=130;}
if(score ==12000){gametime=0;es1=300;es2=220;es3=180;}
if(score >12000&&score<18000){es1=380;es2=270;es3=280;}
if(score ==18000){gametime=0;es1=380;es2=270;es3=280;}
if(score >18000){es1=400;es2=450;es3=550;}

for (var j=0;j<enemy1.length;j++)
{enemy1[j].move(es1*modifier);if(enemy1[j].x<0){enemy1[j].x=1200;enemy1[j].y=(Math.random()*canvas.height)-30;}}
for (var j=0;j<enemy2.length;j++)
{enemy2[j].move(es2*modifier);if(enemy2[j].x<0){enemy2[j].x=1200;enemy2[j].y=(Math.random()*canvas.height)-30;}}
for (var j=0;j<enemy3.length;j++)
{enemy3[j].move(es3*modifier);if(enemy3[j].x<0){enemy3[j].x=1200;enemy3[j].y=(Math.random()*canvas.height)-30;}}

collide();
}
};



var collide = function(){
	if(bready == false)
	{bullets[0].x = bullets[0].x+5;if(bullets[0].x>1200){bullets = [];bready=true;}}
	for(var j = 0;j<enemy1.length;j++){
	if ( player.x <= (enemy1[j].x + 30) && enemy1[j].x <= (player.x + 35) && player.y <= (enemy1[j].y + 30) && enemy1[j].y <= (player.y + 35)) 
	{dead = true; }
	}
	
	for(var j = 0;j<enemy2.length;j++){
	if ( player.x <= (enemy2[j].x + 30) && enemy2[j].x <= (player.x + 35) && player.y <= (enemy2[j].y + 30) && enemy2[j].y <= (player.y + 35)) 
	{dead = true; }
	}
	
	for(var j = 0;j<enemy3.length;j++){
	if ( player.x <= (enemy3[j].x + 50) && enemy3[j].x <= (player.x + 35) && player.y <= (enemy3[j].y + 50) && enemy3[j].y <= (player.y + 35)) 
	{dead = true; }
	}
	if(bready==false){
	for(var j = 0;j<enemy1.length;j++){
	if(bullets[0].x+5 > enemy1[j].x && bullets[0].y>enemy1[j].y&&bullets[0].y<enemy1[j].y+50)
	{enemy1[j].x=1200;bullets = [];bready=true;break;}
	}
	}
	if(bready==false){
		for(var j = 0;j<enemy2.length;j++){
	if(bullets[0].x+5 > enemy2[j].x && bullets[0].y>enemy2[j].y&&bullets[0].y<enemy2[j].y+50)
	{enemy2[j].x=1200;bullets = [];bready=true;break;}
	}
	}
	if(bready==false){
		for(var j = 0;j<enemy3.length;j++){
	if(bullets[0].x+5 > enemy3[j].x && bullets[0].y>enemy3[j].y&&bullets[0].y<enemy3[j].y+80)
	{enemy3[j].x=1200;bullets = [];bready=true;break;}
	}
	}
	

};

var spawn = function(){


if(score == 100*spwn1&&spwn1<86){{spwn1++;if(spwn1 == 15)spwn1=80;
	createEnemies(enemy1,enemy1Image);
}}

if(score == 200*spwn2&&spwn2<86){{spwn2++;if(spwn2 == 15)spwn2=80;
createEnemies(enemy2,enemy2Image);
}}

if(score == 300*spwn3&&spwn3<86){{spwn3++;if(spwn3 == 10)spwn3=80;
createEnemies(enemy3,enemy3Image);}}
};

// Draw everything
var render = function () {
	if(newgm == false){
if (bgReady) {
ctx.drawImage(bgImage, 0, 0); }
if (playerReady) {
ctx.drawImage(playerImage, player.x, player.y); }
if (enemy1Ready) {
for (var j=0;j<enemy1.length;j++)
{enemy1[j].draw();}}
if (enemy2Ready) {
for (var j=0;j<enemy2.length;j++)
{enemy2[j].draw();}}
if (enemy3Ready) {
for (var j=0;j<enemy3.length;j++)
{enemy3[j].draw();}
if(bready == false){
bullets[0].draw();}
}

// Score
ctx.fillStyle = "rgb(250, 250, 250)";
ctx.font = "24px Helvetica";
ctx.textAlign = "left";
ctx.textBaseline = "top";
ctx.fillText("Score : " + score, 550, 32);
ctx.fillStyle = "rgb(250, 250, 250)";
ctx.font = "24px Helvetica";
ctx.textAlign = "left";
ctx.textBaseline = "top";
ctx.fillText("High Score : " + hscore, 900, 32);
ctx.fillStyle = "rgb(250, 250, 250)";
ctx.font = "24px Helvetica";
ctx.textAlign = "left";
ctx.textBaseline = "top";
if(score<5000)ctx.fillText("Level : NOOB", 200, 32);
if(score>5000&&score<18000)ctx.fillText("Level : MEDIUM", 200, 32);
if(score>18000)ctx.fillText("Level : INSANE!!", 200, 32);}

if(newgm==true){ctx.drawImage(menuim, -80, 0);}
};

// The main game loop
var main = function () {
var now = Date.now();
var delta = (now - then)/ 1000;
update(delta ); 
if(dead==false){
render();}
if(dead==true&&q<500){q++;ctx.drawImage(nuke, 0, 0);}
if(dead==true&&q>=500){q=0;dead=false;newgm=true;reset();}
 then = now;
};

// Let's play this game!
reset();
var then = Date.now();
setInterval(main, 1); // Execute as fast ass possible